package com.didispace.requestCacheByComment;

import com.didispace.web.User;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

// @Service
public class UserService extends HystrixCommand<User> {

    private RestTemplate restTemplate;
    private String name;
    private int age;

    public UserService(RestTemplate restTemplate, String name, int age){
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("UserGroup")));
        this.restTemplate = restTemplate;
        this.name = name;
        this.age = age;
    }

    /**
     * 注意本地main方法启动，url请用http://localhost:8081/user2?name={1}&age={2}
     * 通过controller请求启动需要改为服务调用地址：http://eureka-service/user2?name={1}&age={2}
     */
    @Override
    protected User run() throws Exception {
        // return restTemplate.getForObject("http://localhost:8081/users/{1}", User.class, id); // 书上的运行不正确  p152
        //本地请求
        return restTemplate.getForEntity("http://localhost:8081/user2?name={1}&age={2}", User.class, name, age).getBody();
    }

    /**
     * 降级。Hystrix会在run()执行过程中出现错误、超时、线程池拒绝、断路器熔断等情况时， 执行getFallBack()方法内的逻辑
     */
    @Override
    protected User getFallback() {
        return new User("error", 0);
    }

    // 请求缓存  开启请求缓存功能  p159
    // @Override
    // protected String getCacheKey() {
    //     // 根据name入缓存
    //     return name;
    // }

    @CacheResult
    public User getUserByName(String name) {
        return restTemplate.getForObject("http://USER-SERVICE/user4/{l}",
                User.class, name);
    }
}

// 17:27:59.905 [hystrix-UserGroup-1] DEBUG org.springframework.web.client.RestTemplate - Created GET request for "http://localhost:8081/user2?name=aaa&age=15"
//         17:27:59.946 [hystrix-UserGroup-1] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [application/json, application/*+json]
// 17:27:59.971 [hystrix-UserGroup-1] DEBUG org.springframework.web.client.RestTemplate - GET request for "http://localhost:8081/user2?name=aaa&age=15" resulted in 200 (OK)
// 17:27:59.972 [hystrix-UserGroup-1] DEBUG org.springframework.web.client.RestTemplate - Reading [class com.didispace.web.User] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@7ce121a5]
// ------------------name=aaa, age=15
// 17:28:00.001 [hystrix-UserGroup-2] DEBUG org.springframework.web.client.RestTemplate - Created GET request for "http://localhost:8081/user2?name=bbb&age=16"
// 17:28:00.003 [hystrix-UserGroup-2] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [application/json, application/*+json]
// 17:28:00.007 [hystrix-UserGroup-2] DEBUG org.springframework.web.client.RestTemplate - GET request for "http://localhost:8081/user2?name=bbb&age=16" resulted in 200 (OK)
// 17:28:00.007 [hystrix-UserGroup-2] DEBUG org.springframework.web.client.RestTemplate - Reading [class com.didispace.web.User] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@5a55e11e]
// ------------------name=bbb, age=16
// 17:28:00.029 [hystrix-UserGroup-3] DEBUG org.springframework.web.client.RestTemplate - Created GET request for "http://localhost:8081/user2?name=ccc&age=17"
// 17:28:00.030 [hystrix-UserGroup-3] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [application/json, application/*+json]
// 17:28:00.034 [hystrix-UserGroup-3] DEBUG org.springframework.web.client.RestTemplate - GET request for "http://localhost:8081/user2?name=ccc&age=17" resulted in 200 (OK)
// 17:28:00.035 [hystrix-UserGroup-3] DEBUG org.springframework.web.client.RestTemplate - Reading [class com.didispace.web.User] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@501355a1]
// ------------------name=ccc, age=17
// Disconnected from the target VM, address: '127.0.0.1:0', transport: 'socket'
//
// Process finished with exit code 0